<?php

namespace App\Admin\Renderable;

use App\Models\Administrator;
use Dcat\Admin\Grid;
use Dcat\Admin\Grid\LazyRenderable;

class UserTable extends LazyRenderable
{
    public function grid(): Grid
    {
        $userModel = Administrator::query()
            ->whereHas('roles', function ($query) {
                $query->where('slug', 'clue_dispatch');
            });

        return Grid::make($userModel, function (Grid $grid) {
            $grid->column('id');
            $grid->column('name', '用户名');
            $grid->column('created_at');

            // 指定行选择器选中时显示的值的字段名称
            // 指定行选择器选中时显示的值的字段名称
            // 指定行选择器选中时显示的值的字段名称
            // 如果表格数据中带有 “name”、“title”或“username”字段，则可以不用设置
            $grid->rowSelector()->titleColumn('name');

            $grid->quickSearch(['id', 'name']);

            $grid->paginate(100);
            $grid->disableActions();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('username')->width(4);
                $filter->like('name')->width(4);
            });
        });
    }
}
